package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.common.service.FileStorageService;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.manger.dto.BannerDto;
import com.zhentao.model.common.manger.pojo.Activity;
import com.zhentao.model.common.manger.pojo.Announcements;
import com.zhentao.model.common.manger.pojo.Banner;
import com.zhentao.service.BannerService;
import com.zhentao.mapper.BannerMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
* @author lenovo
* @description 针对表【banner】的数据库操作Service实现
* @createDate 2024-09-27 11:16:09
*/
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner>
    implements BannerService{
    @Resource
    private BannerMapper bannerMapper;
    @Resource
    private FileStorageService fileStorageService;

    @Override
    public ResponseResult addBanner(Banner banner) {
        banner.setCreateTime(new Date());
        bannerMapper.insert(banner);
        return ResponseResult.okResult("轮播图添加成功");
    }

    @Override
    public ResponseResult getBanner(BannerDto dto) {
        Page<Banner> page = new Page<>(dto.getPage(),dto.getSize());
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(StringUtils.isNotBlank(dto.getStatus()),Banner::getStatus, dto.getStatus());
        page = bannerMapper.selectPage(page, wrapper);
        return ResponseResult.okResult(page);
    }

    @Override
    public ResponseResult updateBanner(Banner banner) {
        if(banner.getStatus()==1){
            return ResponseResult.okResult(500,"该轮播图正在使用，无法修改");
        }
        bannerMapper.updateById(banner);
        return ResponseResult.okResult("轮播图修改成功");
    }

    @Override
    public ResponseResult deleteBanner(Integer id) {
        Banner banner = bannerMapper.selectById(id);
        if(banner.getStatus()==1){
            return ResponseResult.okResult(500,"该轮播图正在使用，无法删除");
        }
        bannerMapper.deleteById(id);
        return ResponseResult.okResult("轮播图删除成功");
    }


    @Override
    public String file(MultipartFile file) throws IOException {
        String filename= UUID.randomUUID()+file.getOriginalFilename();
        String path = fileStorageService.uploadImage(filename, file.getInputStream());
        return path;
    }

    @Override
    public ResponseResult upAndDown(Integer id) {
        Banner banner = bannerMapper.selectById(id);
        if(banner.getStatus()==1){
            banner.setStatus(2);
            bannerMapper.updateById(banner);
            return ResponseResult.okResult("已启用");
        }else{
            banner.setStatus(1);
            bannerMapper.updateById(banner);
            return ResponseResult.okResult("已禁用");
        }
    }
}




